﻿//Delete item
//sess_pos : position of session product
function delete_item(id, product_image, pd_id){
	
	var anwser = confirm(lang_alert_delete);
	i = id.parentNode.parentNode.rowIndex;
	if(anwser){
		//Delete row of table product
		document.getElementById('table_postnews_product').deleteRow(i);
		//Ajax for process data
		$(document).ready(function(){
			$.ajax({
				 type:"POST",
				 url:site_root_domain+"/index.php?site=postnews&act=delete_product", 
				 data:"product_image="+product_image+"&id="+pd_id,
				 success:function(html){
     					 $("#form_insert").show();
    			 }
    		});	
		
		});
	}
}


//Get value edit & delete
//Edit = 1 : editpage, edit = 0 : addpage
function get_value_edit(id, edit){
	

	//If product has existed in database update data in product table
	if(edit != 0 ){
		document.getElementById('product_edit_save_to_db').value = edit;	
	}

	product_price = document.getElementById("product_price"+id).value;
	product_discount = document.getElementById("product_discount"+id).value;
	product_image = document.getElementById("product_image"+id).value;

	document.getElementById("product_price").value = product_price;
	document.getElementById("product_discount").value = product_discount;
	document.getElementById("product_image_url").value = product_image;

	//Set mode edit
	document.getElementById("product_edit").value = 1;

	 $("#form_insert").show();
	insert_thumb ( '123', product_image, 140, 100 );
	
}

function get_value(id, edit){
	

	//If product has existed in database update data in product table
	if(edit != 0 ){
		document.getElementById('product_edit_save_to_db').value = edit;	
	}

	product_price = document.getElementById("product_price1").value;
	product_discount = document.getElementById("product_discount1").value;
	product_image = document.getElementById("product_image1").value;

	document.getElementById("product_price").value = product_price;
	document.getElementById("product_discount").value = product_discount;
	document.getElementById("product_image_url").value = product_image;
	
	
	document.getElementById("product_price_edit").value = product_price;
	document.getElementById("product_discount_edit").value = product_discount;
	document.getElementById("product_image_url_edit").value = product_image;
	//Set mode edit
	document.getElementById("product_edit").value = 1;

	 $("#form_insert").show();
	insert_thumb ( '123', product_image, 140, 100 );
	
}

//Check empty input

function check_empty_product(id){
	var value =document.getElementById(id).value;
	return (!value) ? false:true;
}

function insert_image( url, width, height, price, price_discount )
	{
		
		//name = "<b>"+postnews_product_name+":"+name+"<b><br/>";
		price_text = "<b>"+postnews_product_price+"&nbsp;:"+price+""+"&nbsp;VNĐ<b><br/>";
		price_remain = (price*(100-price_discount))/100;
		price_remain_text = "<b>"+postnews_product_price_remain+"&nbsp;:"+price_remain+""+"&nbsp;VNĐ<b><br/>";
		parent.tinyMCE.execCommand("mceInsertContent",false,''+price_text+''+price_remain_text+'</b><br/><img border="0" height="'+height+'" src="../uploads/product/'+url+'" width="'+width+'" " />');
	}

function insert_image_edit( url, width, height, price, price_discount )
{
		//name = "<b>"+postnews_product_name+":"+name+"<b><br/>";
		price_text = "<b>"+postnews_product_price+"&nbsp;:"+price+""+"&nbsp;VNĐ<b><br/>";
		price_remain = (price*(100-price_discount))/100;
		price_remain_text = "<b>"+postnews_product_price_remain+"&nbsp;:"+price_remain+""+"&nbsp;VNĐ<b><br/>";
		parent.tinyMCE.execCommand("mceInsertContent",false,''+price_text+''+price_remain+'</b><br/><img border="0" height="'+height+'" src="../uploads/product/'+url+'" width="'+width+'" title="'+name+'" />');
}
//Add to thumb	
function insert_thumb (url){
	document.getElementById("thumb_image_respon").innerHTML = url ;
	
}


function set_empty(){
	$("#status").html('');
	$("#product_price").val('');
	$("#product_image_url").val('');
	$("#product_edit").val('');
	$("#thumb_image_respon").html('');	
	$("#status").html('');
	$("#postnews_product_price_remain").html('');
	$("#product_image_delete").val('');
	$("#product_edit_save_to_db").val('0');
	
}

$(document).ready(function(){
	
   $("#menu_id").change(function(){
    $.ajax({
     type:"POST",
     url:site_root_domain+"/index.php?site=category&act=process_menu", //file ajax.php
     data:"menu="+$("#menu_id").val(),//du lieu gui di
     success:function(html){
      $("#cat_id").html(html);
     }
    });
   });

   //Find keyword
   $("#postnews_cat_id").change(function(){
	
    $.ajax({
     type:"POST",
     url:site_root_domain+"/index.php?site=postnews&act=add", 
     data:"cat_id="+$("#postnews_cat_id").val(),
     success:function(html){
      $("#keyword_respon").html(html);
     }
    });
   });
   
   //Find customer
   $("#cus_info").keyup(function(){
		   
			$.ajax({
			 type:"POST",
			 url:site_root_domain+"/index.php?site=postnews&act=search_customer_shop", 
			 data:"cus_info="+$("#cus_info").val(),
			 success:function(html){
			  $("#table_postnews_cus").html(html);
			 }
			});
   });
   
});
   

$(document).ready(function(){

	$("#product_discount").change(function(){
		
		//Check input price
		var empty_price = check_empty_product('product_price');
		if(empty_price == false){
			$("#product_price").addClass('border_error_postnews_product');
			
			return false;
		}	
		
		var price = $("#product_price").val();
		var product_discount = $("#product_discount").val();	
		var price_remain = (price*(100 - product_discount))/100;
		$("#postnews_product_price_remain").html(""+postnews_product_price_remain+" : "+price_remain+"&nbsp;"+cur_vn+"");
	});
	
	/********** Function click button **********/
   $("#input_submit_postnews_product").click(function(){
	   
	//Process and save data if edit product
	if(document.getElementById('product_edit').value != ''){
		i = document.getElementById('product_edit').value;
	
		//Save image when edit
		html_thumb = '<span id="product_thumb"><img align="left" src="'+site_parent_domain+'/uploads/product/thumbnail/'+$("#product_image_url").val()+'" /></span>';

		
		//Add product name
		$("#td_product_name"+i).html(html_thumb);
		
		html_discount = $("#product_discount").val()
		html_discount += "%";
		$("#td_product_price"+i).html($("#product_price").val());
		$("#td_product_discount"+i).html(html_discount);
		
		//Set hidden value 
		$("#product_name"+i).val($("#product_name").val());
		$("#product_price"+i).val($("#product_price").val());
		$("#product_discount"+i).val($("#product_discount").val());
		
		
		price_discount = ($("#product_discount").val()*$("#product_price").val())/100;
		//Insert image
		//Add image to content
		insert_image( '123', $("#product_image_url").val(), 800, 600, $("#product_name").val(), $("#product_price").val(), price_discount );
		
		//Delete image when editing product, if select file delete past file 
		if($("#product_image_delete").val() != ''){
			product_image_delete = $("#product_image"+i).val();
			$.ajax({
				 type:"POST",
				 url:site_root_domain+"/index.php?site=postnews&act=process_postnews_product_delete", 
				 data:"product_image="+product_image_delete,
			});	
		}
		
		//Set new hidden image
		$("#product_image"+i).val($("#product_image_url").val());
		
		//Delete product in product table if has existed product in product table
		if(  $("#product_edit_save_to_db").val() != 0 ){
				url_edit = $("#product_image_url").val();
				price_edit = $("#product_price").val();
				price_discount_edit = $("#product_discount").val();
				
			$.ajax({
			 type:"POST",
			 url:site_root_domain+"/index.php?site=postnews&act=process_postnews_product_edit", //goi toi file ajax.php
			 data:"product_image="+$("#product_image_url").val()+"&product_price="+$("#product_price").val()+"&product_discount="+$("#product_discount").val()+"&post_id="+$("#post_id").val(),
			 success:function(html){
			
     				$(".tbl_edit").html(html);
							
					$('#product_price_edit').val(price_edit);
					$('#product_image_url_edit').val(url_edit);
					$('#product_discount_edit').val(price_discount_edit);
					//Add image to content
					insert_image_edit( url_edit, 800, 600, price_edit, price_discount_edit );
				
					return false;
    			 }
			});
			set_empty();
			 $("#form_insert").hide();
				//Add thumb

	

			return true;
		
		}
		
		//Update session
		//Respon Content, price, discount
			$.ajax({
			 type:"POST",
			 url:site_root_domain+"/index.php?site=postnews&act=add_pp", //goi toi file ajax.php
			 data:"product_image="+$("#product_image_url").val()+"&product_name="+$("#product_name").val()+"&product_price="+$("#product_price").val()+"&product_discount="+$("#product_discount").val()+"&edit="+i+"&product_total="+$("#product_total").val(),
			 success:function(html){
     					$('.tbl_edit').html(html);
    			 }
			});
		//Set mode edit
		//Set empty input
		set_empty();
			
		 $("#form_insert").hide();
		return ;
	}
	
	//Check input

	$("#product_price").click(function(){
		$("#product_price").removeClass('border_error_postnews_product');
	});
	
	$("#product_image").hover(function(){
		$("#product_image").removeClass('border_error_postnews_product');
	});
	

	if( !check_empty_product('product_price', document.getElementById('product_price').value) ){
		$("#product_price").addClass('border_error_postnews_product');
		return false;
	}
	if( !check_empty_product('product_image_url', document.getElementById('product_image_url').value) ){
		$("#product_image").addClass('border_error_postnews_product');
		return false;
	}
	
	//Respon Content, price, discount  Add first
		
	$.ajax({
     type:"POST",
     url:site_root_domain+"/index.php?site=postnews&act=add", //goi toi file ajax.php
     data:"product_image="+$("#product_image_url").val()+"&product_price="+$("#product_price").val()+"&product_discount="+$("#product_discount").val()+"&product_total="+$("#product_total").val(),//du lieu gui di

     success:function(html){
      $(".block_postnews_product table").append(html);
     }
    });
	
	
	//Add thumb
	
	url = document.getElementById('product_image_url').value;
	price = document.getElementById('product_price').value;
	price_discount = document.getElementById('product_discount').value;
	
	//Add image to content
	insert_image( url, 800, 600, price, price_discount );
	
	//Set empty input
	set_empty();
	$('#form_insert').hide();	
	
  });
  /*-------------------------------------------------*/
  
});
	
 
   
//Upload image

$(function(){
		var btnUpload=$('#product_image');
		var status=$('#status');
		
		new AjaxUpload(btnUpload, {
			action: site_root_domain+"/index.php?site=postnews&act=add",
			name: 'file_upload',
			onSubmit: function(file, ext){
				 if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
                    // extension is not allowed 
					status.text(postnews_product_not_valid);
					return false;
				}
				status.text(postnews_product_uploading);
			},
			onComplete: function(file, response){
				
				//On completion clear the status
				status.text(postnews_product_uploaded);
				//$('#respon_demo').html(response);
				
					
					//Delete odl image if no click save button
					if( $('#product_image_url').val() ){
						$.ajax({
							 type:"POST",
							 url:site_root_domain+"/index.php?site=postnews&act=process_postnews_product_delete", 
							 data:"product_image="+$('#product_image_url').val(),
						});	
					}
					
					//Insert thumb
					insert_thumb ( response );
			
			}
		});
		
	});
	
//Validate postnews

function confirm_validate(url){
	var anwser = confirm(lang_alert_delete);
	
	if( anwser ){
		window.location.href =url;
		
	}
} 

	
	
	